<?php
/*
 * @Author: 李明
 * @Date: 2021-11-27 19:02:51
 * @LastEditTime: 2021-12-10 17:40:39
 * @LastEditors: 李明
 * @Description: 订单提醒功能，后期需要换成  通知 推送
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/order/OrderAlertController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\order;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils;
use App\Models\OaRecord;
use App\Models\Order;
use App\Models\Worker;
use Illuminate\Http\Request;

class OrderAlertController extends Controller
{
    /**
     * @description: 查询订单初审、复审、待修改（个人）、待修改（团队）数量
     * @param {*} $order
     * @return {*}
     */
    public function queryOrderNum(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            $worker_id = $tokenInfo['worker_id'];
        } catch (\Throwable $th) {
            return LMReturn::Error('token存在问题', '400');
        }
        $worker = Worker::with('roles.powers')
            ->find($worker_id);
        // 0、查询管理部门idlist
        $branch_id_list = Utils::getWorkerChildSonBranchs($worker);
        if (count($branch_id_list) == 0) {
            return LMReturn::Error('没有查询权限，无需重复请求', '299');
        }
        // 1、查询是否有 初审权限  初审id是12
        $role =  $worker->roles->first();
        $powers_audit_one = $role->powers->where('id', '12')->all();
        $audit_one = count($powers_audit_one) > 0 ? count(Order::where('audit_step', '1')->where('order_status', '2')->whereIn("branch_id", $branch_id_list)->get()) : 0;

        // 2、查询是否有 复审权限 13
        $powers_audit_two = $role->powers->where('id', '13')->all();
        $audit_two = count($powers_audit_two) > 0 ? count(Order::where('audit_step', '2')->where('order_status', '2')->whereIn("branch_id", $branch_id_list)->get()) : 0;

        // 3、查询是否有待修改（个人）权限  134
        $powers_change_self = $role->powers->where('id', '134')->all();
        $change_self = count($powers_change_self) > 0 ? count(Order::where('order_status', '5')->where('worker_id', $worker_id)->whereIn("branch_id", $branch_id_list)->get()) : 0;
        // 4、查询是否有待修改（团队）权限  85
        $powers_change_all = $role->powers->where('id', '85')->all();
        $change_all = count($powers_change_all) > 0 ? count(Order::where('order_status', '5')->whereIn("branch_id", $branch_id_list)->get()) : 0;

        // // 5、查询是否有待打款 权限  87
        // $powers_change_all = $role->powers->where('id', '85')->all();

        // // 6、查询是否有付款中 权限  88
        // $powers_change_all = $role->powers->where('id', '85')->all();

        // 7、OA 审批提醒
        $oa_records = OaRecord::where('worker_id', $worker_id)->where('is_pass', 0)->whereHas('oa_flow', function ($query){
            $query->whereHas('oa_model', function ($querys) {
                $querys->where('group', 2);
            });
        })->get();
        $oa_count = count($oa_records);

        $resData['audit_one'] = $audit_one;
        $resData['audit_two'] = $audit_two;
        $resData['change_self'] = $change_self;
        $resData['change_all'] = $change_all;
        $resData['oa_records'] = $oa_count;
        return LMReturn::Success($resData, '查询成功');
    }
}
